package com.ltu.service.impl.customize;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.code.kaptcha.Producer;
import com.ltu.ai.sparkdesk.websocket.AiContextManager;
import com.ltu.constant.CommonConstant;
import com.ltu.domain.mp_entity.customize.SessionInfoEntity;
import com.ltu.domain.mp_entity.customize.SessionRecordEntity;
import com.ltu.enums.CommuntiyMenDict;
import com.ltu.enums.CommuntiyMenDict.CommuntiyMenEnum;
import com.ltu.mapper.customize.SessionInfoMapper;
import com.ltu.mapper.customize.SessionRecordMapper;
import com.ltu.model.response.statistic.PubStatisticVO;
import com.ltu.service.customize.SessionInfoService;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ltu.service.impl.BaseServiceImpl;
import com.ltu.util.redis.RedistUtil;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.val;
import me.chanjar.weixin.common.error.WxErrorException;

/**
 * <p>
 * 会话表 服务实现类
 * </p>
 *
 * @author 若尘
 * @since 2024-10-17
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class SessionInfoServiceImpl extends BaseServiceImpl<SessionInfoMapper, SessionInfoEntity>
		implements SessionInfoService {
	private final WxMaService wxMaService;
	private final static String WxMaPage = "pages/index/index";

	@Override
	public String getCurSessionQRCode(SessionInfoEntity session) {
		// TODO Auto-generated method stub
		try {
			CommuntiyMenEnum dict = CommuntiyMenDict.CommuntiyMenEnum.SessionQr;
			String userDir = System.getProperties().getProperty("user.dir").concat(File.separator).concat("static");
			String directory = userDir.concat(File.separator).concat(dict.getValue());
//			String targetName = session.getId().concat(".jpg");
//			String filePath = directory + targetName;
			
			// envVersion : 默认"release" 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"
			System.out.println("文件保存路径："+directory);
			File  qrcodeFile =	wxMaService.getQrcodeService().createWxaCodeUnlimit(session.getId(), WxMaPage, directory, false, "release",
					430, true, null, true);
			String returnPath = dict.getValue().concat("/").concat(qrcodeFile.getName());
			System.out.println("文件访问径："+returnPath);
			session.setWxQrCode(returnPath);
			return returnPath;
		} catch (WxErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";

	}

	@Override
	public List<PubStatisticVO> countField0ByAccount() {
		// TODO Auto-generated method stub
		return this.getBaseMapper().countField0ByAccount();
	}
	

	

}
